﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TuKu.Common;
using System.Text;

namespace Web
{
    public partial class PicList : PageBase
    {
       
        private int pageIndex = 1;
        private int pageSize = 24;
        private int pageCount = -1;
        private int recordCount = -1;
        private AdoPager mm_Pager;
        protected AdoPager m_Pager
        {
            get
            {
                if (mm_Pager == null)
                    mm_Pager = new AdoPager();
                return mm_Pager;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int cateid = PageBase.GetQueryInt("id", 1);
                pageIndex = PageBase.GetQueryInt("pageIndex", 1);
                LoadData(cateid);
                ViewState["cateid"] = cateid.ToString();
                ViewState["map"] = "<a href='index.aspx'>图巨多</a>&gt;<a href='piclist.aspx?id=" + cateid.ToString() + "'>" + map[cateid] + "</a>";
            }
            lblmap.Text = ViewState["map"].ToString();
        }
        private void LoadData(int cateid)
        {
            //string strQuery = "select a.*,b.KindText from tableTest a left join tableKind b on a.KindCode=b.KindCode ";
            string strQuery = "select * from urltable where categoryid=" + cateid.ToString();
            string strShow = "ID,url,title,picpage";

            DataTable dt = m_Pager.ExecutePager(pageIndex, pageSize, strShow, strQuery, "", "ID desc", out pageCount, out recordCount);
            repli.DataSource = dt;
            repli.DataBind();
            Label1.Text = makeSplitePageCode(recordCount, pageSize, pageIndex, pageCount);
            //Label1.Text = string.Format("共{0}条记录，每页{1}条，{2}/{3}", recordCount, pageSize, pageIndex, pageCount);

        }
        //<a class="on">第1页</a><a href="http://tu.pps.tv/star/2.html"  >第2页</a><a href="http://tu.pps.tv/star/3.html"  >第3页</a><a href="http://tu.pps.tv/star/4.html"  >第4页</a><a href="http://tu.pps.tv/star/5.html"  >第5页</a><a href="http://tu.pps.tv/star/6.html"  >第6页</a><a href="http://tu.pps.tv/star/7.html"  >第7页</a><a class="next" href="http://tu.pps.tv/star/2.html">下一页&gt;&gt;</a>
        /// <summary>
        /// 生成分页代码
        /// </summary>
        /// <param name="recordCount">记录数</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="pageIndex">当前页号</param>
        /// <param name="pageCount">总页数</param>
        /// <returns></returns>
        private string makeSplitePageCode(int recordCount, int pageSize, int pageIndex, int pageCount)
        {
            string pagestr = string.Format("共{0}条记录，每页{1}条，{2}/{3}", recordCount, pageSize, pageIndex, pageCount);
            StringBuilder sb = new StringBuilder();
            //省于7页
            if (pageCount <= 7)
            {
                for (int i = 1; i <= pageCount; i++)
                {
                    sb.Append("<a>第" + i.ToString() + "页</a>");
                }
                pagestr = sb.ToString();
            }
            //多于7页
            if (pageCount > 7)
            {
                if (pageIndex <= 7)
                {
                    //前七页
                    for (int i = 1; i <= 7; i++)
                    {
                        if (i == pageIndex)
                        {
                            sb.Append("<a class=\"on\">第" + i.ToString() + "页</a>");
                        }
                        else
                        {
                            sb.Append("<a href='piclist.aspx?id=" + PageBase.GetQueryInt("id", 1).ToString() + "&pageIndex=" + i.ToString() + "'>第" + i.ToString() + "页</a>");
                        }
                    }
                    sb.Append("<a class=\"next\" href='piclist.aspx?id=" + PageBase.GetQueryInt("id", 1).ToString() + "&pageIndex=8'>下一页&gt;&gt;</a>");
                    pagestr = sb.ToString();
                }
                else
                {
                    //中间页
                    if (pageIndex < pageCount - 7)
                    {
                        for (int i = pageIndex - 3; i <= pageIndex + 3; i++)
                        {
                            if (i == pageIndex)
                            {
                                sb.Append("<a class=\"on\">第" + i.ToString() + "页</a>");
                            }
                            else
                            {
                                sb.Append("<a href='piclist.aspx?id=" + PageBase.GetQueryInt("id", 1).ToString() + "&pageIndex=" + i.ToString() + "'>第" + i.ToString() + "页</a>");
                            }

                        }
                        sb.Append("<a class=\"next\" href='piclist.aspx?id=" + PageBase.GetQueryInt("id", 1).ToString() + "&pageIndex=" + (pageIndex + 1).ToString() + "'>下一页&gt;&gt;</a>");
                        pagestr = sb.ToString();
                    }
                    else
                    {
                        for (int i = pageIndex; i < pageCount; i++)
                        {

                            if (i == pageIndex)
                            {
                                sb.Append("<a class=\"on\">第" + i.ToString() + "页</a>");
                            }
                            else
                            {
                                sb.Append("<a href='piclist.aspx?id=" + PageBase.GetQueryInt("id", 1).ToString() + "&pageIndex=" + i.ToString() + "'>第" + i.ToString() + "页</a>");
                            }

                        }
                        sb.Append("<a class=\"next\" href='piclist.aspx?id=" + PageBase.GetQueryInt("id", 1).ToString() + "&pageIndex=1'>第一页&gt;&gt;</a>");
                        pagestr = sb.ToString();
                    }

                }


            }

            return pagestr;
        }


        protected void btnJump_Click(object sender, EventArgs e)
        {
            //int.TryParse(txtPageSize.Text, out pageIndex);
            //LoadData(int.Parse(ViewState["cateid"].ToString()));
        }
    }
}
